<?php 
session_start();
?>
<?php require_once('../../Connections/shopcarda1.php'); ?>
<?php
	if (!function_exists("GetSQLValueString")) {
		function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""){
			if (PHP_VERSION < 6) {
				$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
			}
			$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
			switch ($theType) {
				case "text":
					$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
					break;    
				case "long":
				case "int":
					$theValue = ($theValue != "") ? intval($theValue) : "NULL";
					break;
				case "double":
					$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
					break;
				case "date":
					$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
					break;
				case "defined":
					$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
					break;
			}
			return $theValue;
		}
	}

	$editFormAction = $_SERVER['PHP_SELF'];
	if (isset($_SERVER['QUERY_STRING'])) {
		$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
	}

	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
		$pic_name = "";
		if($_FILES['pic']['tmp_name']!=""){
			$file_name = $_FILES['pic']['name'];
			//分割檔名
			$fn_array=explode(".",$file_name);
			//檔名
			$mainName = $fn_array[0];
			//副檔名	
			$subName = $fn_array[1];
			$files_upload=$_FILES['pic']['tmp_name'];
			$files_name= (int)(date("YmdHi"))+rand(0,99999);
			// 複製上傳圖片到指定 images 目錄
			copy($_FILES['pic']['tmp_name'], "../../admin/product/images/" .$files_name ."." .$subName); 
			$pic_name = $files_name ."." .$subName;
			if($_FILES['pic']['name']==""){
				$pic_name = "";
			}
		}
		$insertSQL = sprintf("INSERT INTO product (prod_no, p_name, add_date, sort, sb_id, price, sell_price, sell_mode, pre_year, pre_month, pre_day, ship_month, ship_day, title, pic, prod_txt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
			GetSQLValueString($_POST['prod_no'], "text"),
			GetSQLValueString($_POST['p_name'], "text"),
			GetSQLValueString(date("Y-m-d"), "date"),
			GetSQLValueString($_POST['sort'], "int"),
			GetSQLValueString($_POST['sb_id'], "int"),
			GetSQLValueString($_POST['price'], "int"),
			GetSQLValueString($_POST['sell_price'], "int"),
			GetSQLValueString($_POST['sell_mode'], "int"),
			GetSQLValueString($_POST['pre_year'], "text"),
			GetSQLValueString($_POST['pre_month'], "text"),
			GetSQLValueString($_POST['pre_day'], "text"),
			GetSQLValueString($_POST['ship_month'], "text"),
			GetSQLValueString($_POST['ship_day'], "text"),
			GetSQLValueString($_POST['title'], "text"),
			GetSQLValueString($pic_name, "text"),
			GetSQLValueString($_POST['prod_txt'], "text"));
			mysql_select_db($database_shopcarda1, $shopcarda1
		);
		$Result1 = mysql_query($insertSQL, $shopcarda1) or die(mysql_error());
		
		$Result2 = mysql_query("select max(p_id) p_ids from product", $shopcarda1) or die(mysql_error());
		$row = @mysql_fetch_assoc ( $Result2);
		for($j=0; $j<count($_POST['attr']);$j++) {
			mysql_query(" insert into prod_attr (p_id, count, attr) values ('".$row['p_ids']."', ".$_POST['count'][$j].", '".$_POST['attr'][$j]."') ", $shopcarda1) or die(mysql_error());
		}

		$insertGoTo = "supplier_product_change_list.php";
		if (isset($_SERVER['QUERY_STRING'])) {
			$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
			$insertGoTo .= $_SERVER['QUERY_STRING'];
		}
		//header(sprintf("Location: %s", $insertGoTo));
		@header("Location:$insertGoTo");
	}

	mysql_select_db($database_shopcarda1, $shopcarda1);
	$query_prod_type_r = "SELECT * FROM prod_type ORDER BY pt_id ASC";
	$prod_type_r = mysql_query($query_prod_type_r, $shopcarda1) or die(mysql_error());
	$row_prod_type_r = mysql_fetch_assoc($prod_type_r);
	$totalRows_prod_type_r = mysql_num_rows($prod_type_r);

	mysql_select_db($database_shopcarda1, $shopcarda1);
	$query_company_member_r = "SELECT cm_id, company FROM company_member ORDER BY cm_id ASC";
	$company_member_r = mysql_query($query_company_member_r, $shopcarda1) or die(mysql_error());
	$row_company_member_r = mysql_fetch_assoc($company_member_r);
	$totalRows_company_member_r = mysql_num_rows($company_member_r);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/supplier.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>新增商品</title>
<!-- InstanceEndEditable -->
<style type="text/css">
<!--
#cantainer {
	width: 955px;
	margin-right: 33px;
	margin-left: 33px;
}
#header {
	width: 955px;
	margin-bottom: 10px;
	background-image: url(../../images/top_bg.png);
	height: 82px;
}
#body {
	width: 955px;
	margin-bottom: 10px;
}
#footer {
	width: 955px;
}
.style2 {color: #666666;
	font-size: 12px;
}
#contant_list {
	float: left;
	width: 180px;
	margin-right: 10px;
}
.style17 {color: #74B9E5; font-size: 14px; }
.style43 {font-size: 12px; color: #666666; font-weight: bold; }
#contant_body {
	float: right;
	width: 765px;
}
-->
</style>
<!-- InstanceBeginEditable name="head" -->
<style type="text/css">
<!--
.style44 {color: #000000}
.style50 {font-size: 9px;
	color: #FF0000;
}
#contant_body_1 {
	background-image: url(../../admin/images/admin_product_add.png);
	width: 737px;
	margin-right: 10px;
	margin-left: 10px;
	height: 25px;
	margin-bottom: 10px;
}
#contant_body_2 {	width: 737px;
	margin-right: 10px;
	margin-bottom: 10px;
	margin-left: 10px;
}
#contant_body_3 {	height: 25px;
	width: 737px;
	margin-right: 10px;
	margin-bottom: 10px;
	margin-left: 10px;
	background-image: url(../../admin/images/admin_product_infor.png);
}
#contant_body_4 {	width: 737px;
	margin-right: 10px;
	margin-bottom: 10px;
	margin-left: 10px;
}
#contant_body_5 {	width: 737px;
	margin-right: 10px;
	margin-left: 10px;
	margin-bottom: 10px;
}
#contant_body_contant {	width: 765px;
	margin-bottom: 10px;
}
#contant_body_header {
	width: 765px;
	margin-bottom: 10px;
	background-image: url(../../admin/images/admin_newproduct_header.png);
	height: 62px;
}
-->
</style>
<!-- InstanceEndEditable -->
<script type="text/javascript" src="../../admin/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../../admin/js/selectboxes.js"></script>
<script>
$(document).ready(function(){
    // 開始產生關聯下拉式選單
    $('#select1').change(function () {
        // 觸發第二階下拉式選單
        $('#sort').removeOption(/.?/).ajaxAddOption(
            '../../admin/product/action.php', 
            { 'val': $(this).val() }, 
            false, 
            function () {
                $(this).selectOptions().trigger('change');
            }
        )
    }).trigger('change');
	// 開始產生關聯下拉式選單
    $('#select2').change(function () {
        // 觸發第二階下拉式選單
        $('#sb_id').removeOption(/.?/).ajaxAddOption(
            '../../admin/product/action2.php', 
            { 'val': $(this).val() }, 
            false, 
            function () {
                $(this).selectOptions().trigger('change');
            }
        )
    }).trigger('change');
});

function changeCount(index , add){
	var count=document.getElementsByName("count[]")[index];
	var value=parseInt(count.value);
	if(add){
		count.value=value+1;	
	}else{
		if(value>0){
			count.value=value-1;
		}
	}
}

function addColumn(){
	var html = '';
	html += '<tr>';
    html += '                                    <td width="8%" align="center" class="style44">新增</td>';
    html += '                                    <td width="39%" align="center">';
    html += '                                        <label>';
    html += '                                        <input type="text" name="attr[]" value="" size="10"/>';
    html += '                                        </label>';
    html += '                                    </td>';
    html += '                                    <td width="33%" align="center">';
    html += '                                        <label>';
    html += '                                        <input name="count[]" type="text" value="" size="10" />';
    html += '                                        </label>';
    html += '                                    </td>';
    html += '                                    <td width="6%">';
    html += '                                        <label>';
    html += '                                        <input type="button" name="button12" id="button12" value="+" onclick="addColumn();"/>';
    html += '                                        </label>';
    html += '                                    </td>';
    html += '                                    <td width="5%">';
    html += '                                        <label>';
    html += '                                        <input type="button" name="button12" id="button13" value="-" onclick="delColumn(this);"/>';
    html += '                                        </label>';
    html += '                                    </td>';
    html += '                                    <td width="9%">&nbsp;</td>';
     html += '                                 </tr>';
	$("#spot").append(html);
}

function delColumn(obj){
	if($("[name='attr[]']").length>1){
		$(obj).parent().parent().parent().remove();
	}
}
</script>
</head>

<body>
		<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1" enctype="multipart/form-data">
<table width="1024" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><div id="cantainer">
      <div id="header"></div>
      <div id="body">
        <div id="contant_list">
          <?php include_once './template/Category.php'; //選單 ?>
        </div>
        <!-- InstanceBeginEditable name="EditRegion1" -->
        <div id="contant_body">
          <div id="contant_body2">
            <div id="contant_body_header"></div>
            <div id="contant_body_contant">
              <table width="765" cellpadding="0" cellspacing="0">
                <tr>
                  <td colspan="3"><img src="../img/member_body_up.jpg" width="765" height="9" /></td>
                </tr>
                <tr>
                  <td width="4" background="../img/member_body_left.jpg">&nbsp;</td>
                  <td width="757"><div id="contant_body_1"></div>
                      <div id="contant_body_2">
                        <table width="737" cellpadding="5" cellspacing="0">
                          <tr>
                            <td align="left" class="style2 style44">商品貨號</td>
                            <td colspan="3" class="style2">&nbsp;</td>
                          </tr>
                          <tr>
                            <td align="left" bgcolor="#E2F8F6" class="style2 style44">商品原始貨號</td>
                            <td colspan="3" bgcolor="#E2F8F6" class="style2">
                          <label>
                            <input type="text" name="prod_no" id="prod_no" />
                            </label>                      </td>
                          </tr>
                          <tr>
                            <td width="120" align="left" class="style2 style44">商品名稱</td>
                            <td colspan="3" class="style2">
                          <label>
                          <input name="p_name" type="text" id="p_name" size="30" />
                          </label>                     </td>
                          </tr>
                          <tr>
                            <td align="left" bgcolor="#E2F8F6" class="style2 style44">商品類別</td>
                            <td width="116" bgcolor="#E2F8F6" class="style2">
                          <label>
                            <select name="select1" id="select1">
                              <?php
do {  
?>
                              <option value="<?php echo $row_prod_type_r['pt_id']?>"><?php echo $row_prod_type_r['type']?></option>
                              <?php
} while ($row_prod_type_r = mysql_fetch_assoc($prod_type_r));
  $rows = mysql_num_rows($prod_type_r);
  if($rows > 0) {
      mysql_data_seek($prod_type_r, 0);
	  $row_prod_type_r = mysql_fetch_assoc($prod_type_r);
  }
?>
                            </select>
                            </label>                      </td>
                            <td width="118" bgcolor="#E2F8F6" class="style2">
                          <label>
                            <select name="sort" id="sort">
</select>
                            </label> 
                            </td>
                            <td width="341" bgcolor="#E2F8F6" class="style2 style44">&nbsp;</td>
                          </tr>
                          <tr>
                            <td align="left" class="style2 style44">製造商</td>
                            <td class="style2 style44"><?php echo $companyMember->{"company"}?><input name="select2" value="<?php echo $companyMember->{"cm_id"}?>" type="hidden" />
                              <label></label></td>
                            <td colspan="2" class="style2 style44">
                          <label>
						  <!--
                            <select name="select2" id="select2">
								<?php
									do {  
								?>
									<option value="<?php echo $row_company_member_r['cm_id']?>"><?php echo $row_company_member_r['company']?></option>
								<?php
									} while ($row_company_member_r = mysql_fetch_assoc($company_member_r));
									$rows = mysql_num_rows($company_member_r);
									if($rows > 0) {
										mysql_data_seek($company_member_r, 0);
										$row_company_member_r = mysql_fetch_assoc($company_member_r);
									}
								?>
                            </select>
							-->
							
							<select name="sb_id" id="sb_id">
							<?php
								mysql_select_db($database_shopcarda1, $shopcarda1);
								$query_link = sprintf("SELECT * FROM supplier_brand WHERE cm_id = %s ORDER BY sb_id ASC", GetSQLValueString($companyMember->{"cm_id"}, "int"));
								$link = mysql_query($query_link, $shopcarda1) or die(mysql_error());
								$row_link = mysql_fetch_assoc($link);
								$totalRows_link = mysql_num_rows($link);
								do {
							?>
								<option value="<?php echo $row_link['sb_id']?>"><?php echo $row_link['logo_name']?></option>
							<?php
									//$arr = array ('pst_id' => $row_link['pst_id'], 'type' => //$row_link['type'], 'sub_type' => $row_link['sub_type']);
									//$list[] = $arr;
									//$data[$row_link['sb_id']] = $row_link['logo_name'];
								} while ($row_link = mysql_fetch_assoc($link));
							?>
							</select>
                            </label>
                          </td>
                          </tr>
                          <tr>
                            <td align="left" bgcolor="#E2F8F6" class="style2 style44">訂價</td>
                            <td colspan="3" bgcolor="#E2F8F6" class="style2">
                          <label>
                            <input name="price" type="text" id="price" size="15" />
                            </label>                     </td>
                      </tr>
                          </tr>
                          <tr>
                            <td align="left" class="style2 style44">售價</td>
                            <td colspan="3" class="style2">
                            <label>
                              <input name="sell_price" type="text" id="sell_price" size="15" />
                              </label>                      </td>
                          </tr>
                          <tr>
                            <td align="left" bgcolor="#E2F8F6" class="style2 style44">販售方式</td>
                            <td colspan="3" bgcolor="#E2F8F6" class="style2"><label>                            </label>
                          <p>
                            <label>
                              <input name="sell_mode" type="radio" id="sell_mode_0" value="1" checked="checked" />
                              正品</label>
                            <label>
                              <input type="radio" name="sell_mode" value="2" id="sell_mode_1" />
                              特價</label>
                            <label>
                              <input type="radio" name="sell_mode" value="3" id="sell_mode_2" />
                              預售</label>
                            &nbsp;
                            <label>
                              <input name="pre_year" type="text" id="pre_year" size="4" />
                              年
                              <select name="pre_month" id="pre_month">
                                <option selected="selected">請選擇</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                                <option value="11">11</option>
                                <option value="12">12</option>
                                </select>
                              月
                              <select name="pre_day" id="pre_day">
                                <option selected="selected">請選擇</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                                <option value="11">11</option>
                                <option value="12">12</option>
                                <option value="13">13</option>
                                <option value="14">14</option>
                                <option value="15">15</option>
                                <option value="16">16</option>
                                <option value="17">17</option>
                                <option value="18">18</option>
                                <option value="19">19</option>
                                <option value="20">20</option>
                                <option value="21">21</option>
                                <option value="22">22</option>
                                <option value="23">23</option>
                                <option value="24">24</option>
                                <option value="25">25</option>
                                <option value="26">26</option>
                                <option value="27">27</option>
                                <option value="28">28</option>
                                <option value="29">29</option>
                                <option value="30">30</option>
                                <option value="31">31</option>
                                </select>
                              日</label></td>
                          </tr>
                          <tr>
                            <td align="left" class="style2 style44">商品 尺寸/數量<br />
                                <span class="style50">**如無尺寸請於尺寸欄位填寫單一尺寸</span></td>
                            <td colspan="3" class="style2"><table width="100%" cellspacing="0" cellpadding="5">
                                <tr>
                                  <td width="45" bgcolor="#CCCCCC">&nbsp;</td>
                                  <td width="212" bgcolor="#CCCCCC"><div align="center">尺寸</div></td>
                                  <td width="181" bgcolor="#CCCCCC"><div align="center">數量</div></td>
                                  <td width="115" bgcolor="#CCCCCC">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td colspan="4"><table width="100%" cellspacing="0" cellpadding="5">
                                      <tr>
                                        <td width="8%" align="center" class="style44">1</td>
                                        <td width="39%" align="center">
                                            <label>
                                            <input name="attr[]" type="text" id="textfield8" size="10" />
                                            </label>
                                        </td>
                                        <td width="33%" align="center">
                                            <label>
                                            <input name="count[]" type="text" id="textfield9" size="10" value="1" />
                                            </label>
                                        </td>
                                        <td width="6%">
                                            <label>
                                            <input type="button" name="button9" id="button9" value="+" onclick="addColumn();"/>
                                            </label>
                                        </td>
                                        <td width="5%">
                                            <label>
                                            <input type="button" name="button10" id="button10" value="-" onclick="delColumn(this);"/>
                                            </label>
                                        </td>
                                        <td width="9%">&nbsp;</td>
                                      </tr>
                                    </table>
                                      <table id="spot" width="100%" cellspacing="0" cellpadding="5">
                                        <tr>
                                          <td width="8%" align="center" class="style44">2</td>
                                          <td width="39%" align="center">
                                              <label>
                                              <input name="attr[]" type="text" id="textfield10" size="10" />
                                              </label>
                                          </td>
                                          <td width="33%" align="center">
                                              <label>
                                              <input name=count[] type="text" id="textfield11" size="10" value="0"/>
                                              </label>
                                          </td>
                                          <td width="6%">
                                              <label>
                                              <input type="button" name="button12" id="button12" value="+" onclick="addColumn();"/>
                                              </label>
                                          </td>
                                          <td width="5%">
                                              <label>
                                              <input type="button" name="button12" id="button13" value="-" onclick="delColumn(this);"/>
                                              </label>
                                          </td>
                                          <td width="9%">&nbsp;</td>
                                        </tr>
                                    </table></td>
                                </tr>
                            </table></td>
                          </tr>
                          <tr>
                            <td align="left" bgcolor="#E2F8F6" class="style2 style44">出貨時間</td>
                            <td colspan="3" bgcolor="#E2F8F6" class="style2"><label></label>
                          <label>
                            <select name="ship_month" id="ship_month">
                              <option selected="selected">請選擇</option>
                              <option value="1">1個</option>
                              <option value="2">2個</option>
                              <option value="3">3個</option>
                              <option value="4">4個</option>
                              <option value="5">5個</option>
                              <option value="6">6個</option>
                              <option value="7">7個</option>
                              <option value="8">8個</option>
                              <option value="9">9個</option>
                              <option value="10">10個</option>
                              <option value="11">11個</option>
                              <option value="12">12個</option>
                            </select>
                            </label>
                          月
                          <label>
                            <select name="ship_day" id="ship_day">
                              <option selected="selected">請選擇</option>
                              <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                <option value="6">6</option>
                                <option value="7">7</option>
                                <option value="8">8</option>
                                <option value="9">9</option>
                                <option value="10">10</option>
                                <option value="11">11</option>
                                <option value="12">12</option>
                                <option value="13">13</option>
                                <option value="14">14</option>
                                <option value="15">15</option>
                                <option value="16">16</option>
                                <option value="17">17</option>
                                <option value="18">18</option>
                                <option value="19">19</option>
                                <option value="20">20</option>
                                <option value="21">21</option>
                                <option value="22">22</option>
                                <option value="23">23</option>
                                <option value="24">24</option>
                                <option value="25">25</option>
                                <option value="26">26</option>
                                <option value="27">27</option>
                                <option value="28">28</option>
                                <option value="29">29</option>
                                <option value="30">30</option>
                                <option value="31">31</option>
                            </select>
                            </label>
                          日                      </td>
                          </tr>
                        </table>
                      </div>
                    <div id="contant_body_3"></div>
                    <div id="contant_body_4">
                        <table width="737" cellspacing="0" cellpadding="5">
                          <tr>
                            <td width="120" align="left" valign="top" class="style2 style44">標題</td>
                            <td width="595" class="style2">
                            <label>
                            <textarea name="title" id="title" cols="45" rows="2"></textarea>
                            </label>
                        </td>
                          </tr>
                          <tr bgcolor="#E2F8F6">
                            <td align="left" valign="top" class="style2 style44">小圖上傳</td>
                            <td class="style2"><table width="100%" cellspacing="0" cellpadding="5">
                            <tr>
                              <td width="41%" class="style44">
                                  <label>
                                  <input name="pic" type="file" id="pic" size="30" />
                                  </label>
                              </td>
                              <td width="48%" class="style44">1:1</td>
                            </tr>
                        </table></td>
                          </tr>
                          <tr>
                            <td align="left" valign="top" class="style2 style44">介紹內容</td>
                           <td class="style2">
                            <label>
                            <textarea name="prod_txt" id="prod_txt" cols="65" rows="20"></textarea>
                            </label>
                        </td>
                          </tr>
                        </table>
                    </div>
                    <div id="contant_body_5">
                        <table width="100%" cellspacing="0" cellpadding="0">
                          <tr>
                            <td align="center">
                                <label>
                                <input type="submit" name="button7" id="button7" value="新增" />
                                </label>
                                <label>
                                <input type="submit" name="button8" id="button8" value="重填" />
                                </label>
                            </td>
                          </tr>
                        </table>
                    </div></td>
                  <td width="4" background="../img/member_body_right.jpg">&nbsp;</td>
                </tr>
                <tr>
                  <td height="9" colspan="3"><img src="../img/member_body_down.jpg" width="765" height="9" /></td>
                </tr>
              </table>
            </div>
          </div>
        </div>
		        <input type="hidden" name="MM_insert" value="form1" />
        <?php
include_once "../../admin/product/ckeditor/ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = '../../admin/product/ckeditor/';
$CKEditor->replace("prod_txt");
?>
        <!-- InstanceEndEditable --></div>
      <div id="footer">
        <?php include_once './template/footer.php'; //地 ?>
      </div>
      </div></td>
  </tr>
</table>
        </form>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($prod_type_r);

mysql_free_result($company_member_r);
?>